package cn.tqfeiyang.smartadmin.system.controller;

import cn.tqfeiyang.smartadmin.system.service.OperationLogService;
import cn.tqfeiyang.smartadmin.system.request.OperationLogCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.OperationLogUpdateDTO;
import cn.tqfeiyang.smartadmin.system.request.OperationLogQueryDTO;
import cn.tqfeiyang.smartadmin.system.response.OperationLogQueryVO;
import cn.tqfeiyang.smartadmin.system.response.OperationLogDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

/**
 * 操作记录控制器
 *
 * @author tqfeiyang
 * @since 2024-08-23
 */
@RestController
@Validated
@RequestMapping("/api/system/operationLog")
@Tag(name = "系统/操作记录")
public class OperationLogController {

    /**
     * 操作记录服务
     */
    @Resource
    private OperationLogService operationLogService;

    /**
     * 查询操作记录
     *
     * @param dto 请求数据
     * @return 操作记录列表
     */
    @OperationLog
    @GetMapping("/query")
    @Operation(summary = "查询操作记录")
    @SaCheckPermission("system:operationLog:query")
    public ApiResult<PageData<OperationLogQueryVO>> queryOperationLogs(@Validated OperationLogQueryDTO dto) {
        return ApiResult.success(operationLogService.queryOperationLogs(dto));
    }

    /**
     * 获取操作记录
     *
     * @param logId 操作记录id
     * @return 操作记录信息
     */
    @OperationLog
    @GetMapping("/{logId}/read")
    @Operation(summary = "获取操作记录")
    @SaCheckPermission("system:operationLog:read")
    public ApiResult<OperationLogDetailVO> readOperationLog(@PathVariable @EntityId Long logId) {
        return ApiResult.success(operationLogService.readOperationLog(logId));
    }

    /**
     * 添加操作记录
     *
     * @param dto 请求数据
     * @return 操作记录信息
     */
    @OperationLog
    @PostMapping("/create")
    @Operation(summary = "添加操作记录")
    @SaCheckPermission("system:operationLog:create")
    public ApiResult<OperationLogDetailVO> createOperationLog(@RequestBody @Validated OperationLogCreateDTO dto) {
        Long logId = operationLogService.createOperationLog(dto);
        return ApiResult.success(operationLogService.readOperationLog(logId));
    }

    /**
     * 修改操作记录
     *
     * @param logId 操作记录id
     * @param dto 请求数据
     * @return 操作记录信息
     */
    @OperationLog
    @PostMapping("/{logId}/update")
    @Operation(summary = "修改操作记录")
    @SaCheckPermission("system:operationLog:update")
    public ApiResult<OperationLogDetailVO> updateOperationLog(@PathVariable @EntityId Long logId, @RequestBody @Validated OperationLogUpdateDTO dto) {
        operationLogService.updateOperationLog(logId, dto);
        return ApiResult.success(operationLogService.readOperationLog(logId));
    }

    /**
     * 删除操作记录
     *
     * @param logId 操作记录id
     */
    @OperationLog
    @PostMapping("/{logId}/delete")
    @Operation(summary = "删除操作记录")
    @SaCheckPermission("system:operationLog:delete")
    public ApiResult<String> deleteOperationLog(@PathVariable @EntityId Long logId) {
        operationLogService.deleteOperationLog(logId);
        return ApiResult.success();
    }
}
